import {
  Controller,
  Post,
  Get,
  Body,
  UseFilters,
  // Req,
  // UnauthorizedException,
} from '@nestjs/common';
// import type { Request } from 'express';
import { Public } from '../../common/decorators/public.decorator';
import { AuthService } from './auth.service';
import { HttpExceptionFilter } from '../../exceptions/http-exception.filter';

@Controller('auth')
@UseFilters(HttpExceptionFilter) // 应用全局错误过滤器
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Public() // 标记为公共路由，登录不需要认证
  @Post('login')
  async login(@Body() bodys: { username: string; password: string }) {
    // 验证输入参数
    if (!bodys.username || !bodys.password) {
      throw new Error('用户名和密码不能为空');
    }

    return this.authService.login(bodys.username, bodys.password);
  }

  @Get('test-error')
  testError() {
    // 测试错误过滤器
    throw new Error('这是一个测试错误');
  }

  @Get('test-http-error')
  testHttpError() {
    // 测试HTTP错误过滤器
    throw new Error('HTTP错误测试');
  }
}
